package handler

import "C"
import (
	"github.com/gin-gonic/gin"
	"gomicrodemo/changgou-api-gateway/good/model"
	"gomicrodemo/common"
	"gomicrodemo/proto/goods"
	"log"
)

type Good_APIHandler struct {
	goodClient goods.GoodService
}

//handler相当于grpc里面的controller
func GetGoodAPIHandler(goodService goods.GoodService) *Good_APIHandler {
	return &Good_APIHandler{
		goodClient: goodService,
	}
}

func (g Good_APIHandler) GetBrands(c *gin.Context) {
	rid := common.Print_RequestId(c)
	var (
		req model.BrandRequest
		rsp *goods.GetBrandsResponse
		err error
	)

	if err = c.ShouldBindJSON(&req); err != nil {
		log.Printf("ShouldBindJson fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": rid,
		})
		return
	}

	//调用微服务获取品牌列表
	rsp, err = g.goodClient.GetBrands(c, &goods.GetBrandsRequest{
		Brand: &goods.Brand{
			Id:     req.Id,
			Name:   req.Name,
			Image:  req.Image,
			Letter: req.Letter,
			Seq:    req.Seq,
		},
		Limit:  req.Limit,
		Offset: req.Offset,
	})
	if err != nil {
		log.Printf("GetBrands fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": common.Print_RequestId(c),
		})
		return
	}
	if rsp.Code != 0 {
		log.Printf("GetBrands fail, msg:%v", rsp.Msg)
		c.JSON(500, gin.H{
			"error":     rsp.Msg,
			"RequestId": common.Print_RequestId(c),
		})
		return
	}
	c.JSON(200, gin.H{
		"Code":      0,
		"Msg":       "success",
		"Data":      rsp.Brands,
		"RequestId": common.Print_RequestId(c),
	})
	return
}

func (g Good_APIHandler) UpdateBrands(c *gin.Context) {
	rid := common.Print_RequestId(c)
	var (
		req model.BrandRequest
		rsp *goods.UpdateBrandsResponse
		err error
	)
	if err = c.ShouldBindJSON(&req); err != nil {
		log.Printf("ShouldBindJson fail, msg:%v", rsp.Msg)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": rid,
		})
		return
	}
	//调用微服务获取品牌列表
	rsp, err = g.goodClient.UpdateBrands(c, &goods.UpdateBrandsRequest{
		Brand: &goods.Brand{
			Id:     req.Id,
			Name:   req.Name,
			Image:  req.Image,
			Letter: req.Letter,
			Seq:    req.Seq,
		},
	})
	if err != nil {
		log.Printf("GetBrands fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": common.Print_RequestId(c),
		})
		return
	}

	if rsp.Code != 0 {
		log.Printf("GetBrands fail, msg:%v", rsp.Msg)
		c.JSON(500, gin.H{
			"error":     rsp.Msg,
			"RequestId": common.Print_RequestId(c),
		})
		return
	}

	c.JSON(200, gin.H{
		"Code":      0,
		"Msg":       "success",
		"RequestId": common.Print_RequestId(c),
	})
	return
}

func (g Good_APIHandler) CreateBrands(c *gin.Context) {
	rid := common.Print_RequestId(c)
	var (
		req model.BrandRequest
		rsp *goods.CreateBrandsResponse
		err error
	)

	if err = c.ShouldBindJSON(&req); err != nil {
		log.Printf("ShouldBindJson fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": rid,
		})
		return
	}

	//调用微服务获取品牌列表
	rsp, err = g.goodClient.CreateBrands(c, &goods.CreateBrandsRequest{
		Brand: &goods.Brand{
			Id:     req.Id,
			Name:   req.Name,
			Image:  req.Image,
			Letter: req.Letter,
			Seq:    req.Seq,

		},
	})
	if err != nil {
		log.Printf("GetBrands fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": common.Print_RequestId(c),
		})
		return
	}

	if rsp.Code != 0 {
		log.Printf("GetBrands fail, msg:%v", rsp.Msg)
		c.JSON(500, gin.H{
			"error":     rsp.Msg,
			"RequestId": common.Print_RequestId(c),
		})
		return
	}

	c.JSON(200, gin.H{
		"Code":      0,
		"Msg":       "success",
		"RequestId": common.Print_RequestId(c),
	})
	return
}

func (g Good_APIHandler) GetTemplates(c *gin.Context) {
	rid := common.Print_RequestId(c)
	var (
		req model.Template
		rsp *goods.GetTemplatesResponse
		err error
	)

	if err = c.ShouldBindJSON(&req); err != nil {
		log.Printf("ShouldBindJson fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": rid,
		})
		return
	}

	//调用微服务获取品牌列表
	rsp, err = g.goodClient.GetTemplates(c, &goods.GetTemplatesRequest {
		Template: &goods.Template{
			Id      :     req.Id,
			Name    :   req.Name,
			SpecNum :  req.SpecNum,
			ParaNum : req.ParaNum,
		},
		Limit:  req.Limit,
		Offset: req.Offset,
	})
	if err != nil {
		log.Printf("GetTemplates fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": common.Print_RequestId(c),
		})
		return
	}

	if rsp.Code != 0 {
		log.Printf("GetTemplates fail, msg:%v", rsp.Msg)
		c.JSON(500, gin.H{
			"error":     rsp.Msg,
			"RequestId": common.Print_RequestId(c),
		})
		return
	}

	c.JSON(200, gin.H{
		"Code":      0,
		"Msg":       "success",
		"Data":      rsp.Templates,
		"RequestId": common.Print_RequestId(c),
	})
	return
}

func (g Good_APIHandler) UpdateTemplate(c *gin.Context) {
	rid := common.Print_RequestId(c)
	var (
		req model.Template
		rsp *goods.UpdateTemplateResponse
		err error
	)
	if err = c.ShouldBindJSON(&req); err != nil {
		log.Printf("ShouldBindJson fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": rid,
		})
		return
	}
	//调用微服务获取品牌列表
	rsp, err = g.goodClient.UpdateTemplate(c, &goods.UpdateTemplateRequest{
		Template: &goods.Template{
			Id:      req.Id,
			Name:    req.Name,
			SpecNum: req.SpecNum,
			ParaNum: req.ParaNum,
		},
	})
	if err != nil {
		log.Printf("UpdateTemplate fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": common.Print_RequestId(c),
		})
		return
	}
	if rsp.Code != 0 {
		log.Printf("UpdateTemplate fail, msg:%v", rsp.Msg)
		c.JSON(500, gin.H{
			"error":     rsp.Msg,
			"RequestId": common.Print_RequestId(c),
		})
		return
	}
	c.JSON(200, gin.H{
		"Code":      0,
		"Msg":       "success",
		"RequestId": common.Print_RequestId(c),
	})
	return
}

func (g Good_APIHandler) CreateTemplate(c *gin.Context) {
	rid := common.Print_RequestId(c)
	var (
		req model.Template
		rsp *goods.CreateTemplateResponse
		err error
	)

	if err = c.ShouldBindJSON(&req); err != nil {
		log.Printf("ShouldBindJson fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": rid,
		})
		return
	}
	//调用微服务获取品牌列表
	rsp, err = g.goodClient.CreateTemplate(c, &goods.CreateTemplateRequest{
		Template: &goods.Template{
			Id:      req.Id,
			Name:    req.Name,
			SpecNum: req.SpecNum,
			ParaNum: req.ParaNum,
		},
	})
	if err != nil {
		log.Printf("CreateTemplate fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": common.Print_RequestId(c),
		})
		return
	}
	if rsp.Code != 0 {
		log.Printf("CreateTemplate fail, msg:%v", rsp.Msg)
		c.JSON(500, gin.H{
			"error":     rsp.Msg,
			"RequestId": common.Print_RequestId(c),
		})
		return
	}
	c.JSON(200, gin.H{
		"Code":      0,
		"Msg":       "success",
		"RequestId": common.Print_RequestId(c),
	})
	return
}

func (g Good_APIHandler) GetParams(c *gin.Context) {
	rid := common.Print_RequestId(c)
	var (
		req model.Param
		rsp *goods.GetParamsResponse
		err error
	)

	if err = c.ShouldBindJSON(&req); err != nil {
		log.Printf("ShouldBindJson fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": rid,
		})
		return
	}

	//调用微服务获取品牌列表
	rsp, err = g.goodClient.GetParams(c, &goods.GetParamsRequest {
		Param: &goods.Param{
			Id:         req.Id,
			Name:       req.Name,
			Options:    req.Options,
			Seq:        req.Seq,
			TemplateId: req.TemplateId,
		},
		Limit:  req.Limit,
		Offset: req.Offset,
	})
	if err != nil {
		log.Printf("GetParams fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": common.Print_RequestId(c),
		})
		return
	}

	if rsp.Code != 0 {
		log.Printf("GetParams fail, msg:%v", rsp.Msg)
		c.JSON(500, gin.H{
			"error":     rsp.Msg,
			"RequestId": common.Print_RequestId(c),
		})
		return
	}

	c.JSON(200, gin.H{
		"Code":      0,
		"Msg":       "success",
		"Data":      rsp.Params,
		"RequestId": common.Print_RequestId(c),
	})
	return
}

func (g Good_APIHandler) UpdateParam(c *gin.Context) {
	rid := common.Print_RequestId(c)
	var (
		req model.Param
		rsp *goods.UpdateParamResponse
		err error
	)
	if err = c.ShouldBindJSON(&req); err != nil {
		log.Printf("ShouldBindJson fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": rid,
		})
		return
	}
	//调用微服务获取品牌列表
	rsp, err = g.goodClient.UpdateParam(c, &goods.UpdateParamRequest{
		Param: &goods.Param{
			Id:         req.Id,
			Name:       req.Name,
			Options:    req.Options,
			Seq:        req.Seq,
			TemplateId: req.TemplateId,
		},
	})
	if err != nil {
		log.Printf("UpdateParam fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": common.Print_RequestId(c),
		})
		return
	}
	if rsp.Code != 0 {
		log.Printf("UpdateParam fail, msg:%v", rsp.Msg)
		c.JSON(500, gin.H{
			"error":     rsp.Msg,
			"RequestId": common.Print_RequestId(c),
		})
		return
	}
	c.JSON(200, gin.H{
		"Code":      0,
		"Msg":       "success",
		"RequestId": common.Print_RequestId(c),
	})
	return
}

func (g Good_APIHandler) CreateParam(c *gin.Context) {
	rid := common.Print_RequestId(c)
	var (
		req model.Param
		rsp *goods.CreateParamResponse
		err error
	)

	if err = c.ShouldBindJSON(&req); err != nil {
		log.Printf("ShouldBindJson fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": rid,
		})
		return
	}
	//调用微服务获取品牌列表
	rsp, err = g.goodClient.CreateParam(c, &goods.CreateParamRequest{
		Param: &goods.Param{
			Id:         req.Id,
			Name:       req.Name,
			Options:    req.Options,
			Seq:        req.Seq,
			TemplateId: req.TemplateId,
		},
	})
	if err != nil {
		log.Printf("CreateParam fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": common.Print_RequestId(c),
		})
		return
	}
	if rsp.Code != 0 {
		log.Printf("CreateParam fail, msg:%v", rsp.Msg)
		c.JSON(500, gin.H{
			"error":     rsp.Msg,
			"RequestId": common.Print_RequestId(c),
		})
		return
	}
	c.JSON(200, gin.H{
		"Code":      0,
		"Msg":       "success",
		"RequestId": common.Print_RequestId(c),
	})
	return
}

func (g Good_APIHandler) GetSpecification(c *gin.Context) {
	rid := common.Print_RequestId(c)
	var (
		req model.Param
		rsp *goods.GetSpecificationsResponse
		err error
	)

	if err = c.ShouldBindJSON(&req); err != nil {
		log.Printf("ShouldBindJson fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": rid,
		})
		return
	}

	//调用微服务获取品牌列表
	rsp, err = g.goodClient.GetSpecifications(c, &goods.GetSpecificationsRequest {
		Specification: &goods.Specification{
			Id:         req.Id,
			Name:       req.Name,
			Options:    req.Options,
			Seq:        req.Seq,
			TemplateId: req.TemplateId,
		},
		Limit:  req.Limit,
		Offset: req.Offset,
	})
	if err != nil {
		log.Printf("GetSpecifications fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": common.Print_RequestId(c),
		})
		return
	}

	if rsp.Code != 0 {
		log.Printf("GetSpecifications fail, msg:%v", rsp.Msg)
		c.JSON(500, gin.H{
			"error":     rsp.Msg,
			"RequestId": common.Print_RequestId(c),
		})
		return
	}

	c.JSON(200, gin.H{
		"Code":      0,
		"Msg":       "success",
		"Data":      rsp.Specifications,
		"RequestId": common.Print_RequestId(c),
	})
	return
}

func (g Good_APIHandler) UpdateSpecification(c *gin.Context) {
	rid := common.Print_RequestId(c)
	var (
		req model.Param
		rsp *goods.UpdateSpecificationResponse
		err error
	)
	if err = c.ShouldBindJSON(&req); err != nil {
		log.Printf("ShouldBindJson fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": rid,
		})
		return
	}
	//调用微服务获取品牌列表
	rsp, err = g.goodClient.UpdateSpecification(c, &goods.UpdateSpecificationRequest{
		Specification: &goods.Specification{
			Id:         req.Id,
			Name:       req.Name,
			Options:    req.Options,
			Seq:        req.Seq,
			TemplateId: req.TemplateId,
		},
	})
	if err != nil {
		log.Printf("UpdateSpecification fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": common.Print_RequestId(c),
		})
		return
	}
	if rsp.Code != 0 {
		log.Printf("UpdateSpecification fail, msg:%v", rsp.Msg)
		c.JSON(500, gin.H{
			"error":     rsp.Msg,
			"RequestId": common.Print_RequestId(c),
		})
		return
	}
	c.JSON(200, gin.H{
		"Code":      0,
		"Msg":       "success",
		"RequestId": common.Print_RequestId(c),
	})
	return
}

func (g Good_APIHandler) CreateSpecification(c *gin.Context) {
	rid := common.Print_RequestId(c)
	var (
		req model.Specification
		rsp *goods.CreateSpecificationResponse
		err error
	)

	if err = c.ShouldBindJSON(&req); err != nil {
		log.Printf("ShouldBindJson fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": rid,
		})
		return
	}
	//调用微服务获取品牌列表
	rsp, err = g.goodClient.CreateSpecification(c, &goods.CreateSpecificationRequest{
		Specification: &goods.Specification{
			Id:         req.Id,
			Name:       req.Name,
			Options:    req.Options,
			Seq:        req.Seq,
			TemplateId: req.TemplateId,
		},
	})
	if err != nil {
		log.Printf("CreateSpecification fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": common.Print_RequestId(c),
		})
		return
	}
	if rsp.Code != 0 {
		log.Printf("CreateSpecification fail, msg:%v", rsp.Msg)
		c.JSON(500, gin.H{
			"error":     rsp.Msg,
			"RequestId": common.Print_RequestId(c),
		})
		return
	}
	c.JSON(200, gin.H{
		"Code":      0,
		"Msg":       "success",
		"RequestId": common.Print_RequestId(c),
	})
	return
}

func (g Good_APIHandler) GetCategorys(c *gin.Context) {
	rid := common.Print_RequestId(c)
	var (
		req model.Categoty
		rsp *goods.GetCategorysResponse
		err error
	)

	if err = c.ShouldBindJSON(&req); err != nil {
		log.Printf("ShouldBindJson fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": rid,
		})
		return
	}

	//调用微服务获取品牌列表
	rsp, err = g.goodClient.GetCategorys(c, &goods.GetCategorysRequest {
		Category: &goods.Category{
			Id:         req.Id,
			Name:       req.Name,
			GoodsNum:   req.GoodsNum,
			IsShow:     req.IsShow,
			IsMenu:     req.IsMenu,
			Seq:        req.Seq,
			ParentId:   req.ParentId,
			TemplateId: req.TemplateId,
		},
		Limit:  req.Limit,
		Offset: req.Offset,
	})
	if err != nil {
		log.Printf("GetCategorys fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": common.Print_RequestId(c),
		})
		return
	}

	if rsp.Code != 0 {
		log.Printf("GetCategorys fail, msg:%v", rsp.Msg)
		c.JSON(500, gin.H{
			"error":     rsp.Msg,
			"RequestId": common.Print_RequestId(c),
		})
		return
	}

	c.JSON(200, gin.H{
		"Code":      0,
		"Msg":       "success",
		"Data":      rsp.Categorys,
		"RequestId": common.Print_RequestId(c),
	})
	return
}

func (g Good_APIHandler) UpdateCategory(c *gin.Context) {
	rid := common.Print_RequestId(c)
	var (
		req model.Categoty
		rsp *goods.UpdateCategoryResponse
		err error
	)
	if err = c.ShouldBindJSON(&req); err != nil {
		log.Printf("ShouldBindJson fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": rid,
		})
		return
	}
	//调用微服务获取品牌列表
	rsp, err = g.goodClient.UpdateCategory(c, &goods.UpdateCategoryRequest{
		Category: &goods.Category{
			Id:         req.Id,
			Name:       req.Name,
			GoodsNum:   req.GoodsNum,
			IsShow:     req.IsShow,
			IsMenu:     req.IsMenu,
			Seq:        req.Seq,
			ParentId:   req.ParentId,
			TemplateId: req.TemplateId,
		},
	})
	if err != nil {
		log.Printf("UpdateCategory fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": common.Print_RequestId(c),
		})
		return
	}
	if rsp.Code != 0 {
		log.Printf("UpdateCategory fail, msg:%v", rsp.Msg)
		c.JSON(500, gin.H{
			"error":     rsp.Msg,
			"RequestId": common.Print_RequestId(c),
		})
		return
	}
	c.JSON(200, gin.H{
		"Code":      0,
		"Msg":       "success",
		"RequestId": common.Print_RequestId(c),
	})
	return
}

func (g Good_APIHandler) CreateCategory(c *gin.Context) {
	rid := common.Print_RequestId(c)
	var (
		req model.Categoty
		rsp *goods.CreateCategoryResponse
		err error
	)

	if err = c.ShouldBindJSON(&req); err != nil {
		log.Printf("ShouldBindJson fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": rid,
		})
		return
	}
	//调用微服务获取品牌列表
	rsp, err = g.goodClient.CreateCategory(c, &goods.CreateCategoryRequest{
		Category: &goods.Category{
			Id:         req.Id,
			Name:       req.Name,
			GoodsNum:   req.GoodsNum,
			IsShow:     req.IsShow,
			IsMenu:     req.IsMenu,
			Seq:        req.Seq,
			ParentId:   req.ParentId,
			TemplateId: req.TemplateId,
		},
	})
	if err != nil {
		log.Printf("CreateCategory fail, msg:%v", err)
		c.JSON(500, gin.H{
			"error":     err.Error(),
			"RequestId": common.Print_RequestId(c),
		})
		return
	}
	if rsp.Code != 0 {
		log.Printf("CreateCategory fail, msg:%v", rsp.Msg)
		c.JSON(500, gin.H{
			"error":     rsp.Msg,
			"RequestId": common.Print_RequestId(c),
		})
		return
	}
	c.JSON(200, gin.H{
		"Code":      0,
		"Msg":       "success",
		"RequestId": common.Print_RequestId(c),
	})
	return
}